package ha.web.servlet.login;

import ha.web.database.DBConnector;
import ha.web.database.UserId;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/Register")
public class Register extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    private boolean somethingWrong = false;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private void LoginRejected (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
    	doGet(request, response);
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        //check input
        Map<String,String[]> requestParm = request.getParameterMap();
        if(requestParm.size()!=3) {
        	LoginRejected(request, response);
        	return;
        }
        String userName,userPass,reUserPass;
        if (!request.getParameterMap().containsKey("user") ||
        		!request.getParameterMap().containsKey("pass") ||
        		!request.getParameterMap().containsKey("re_pass")) {
        	LoginRejected(request, response);
        	return;
        }
        userName = request.getParameter("user");
        userPass = request.getParameter("pass");
        reUserPass = request.getParameter("re_pass");
        if(!userPass.equals(reUserPass) || !DBConnector.addUserAtDB(new UserId(userName, userPass))) {
        	LoginRejected(request, response);
        	return;
        }
        HttpSession session = request.getSession(true);        	
        session.setAttribute("user_visit", userName);
        ServletContext context = getServletContext();
        RequestDispatcher dispatcher = context.getRequestDispatcher("/SuccessLogin");
        dispatcher.forward(request, response);
        request.setAttribute("user_visit", userName);
        doGet(request, response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
		out.print("<HTML><HEAD><TITLE>login page</TITLE>");
		out.print("<META http-equiv=Content-Type content=\"text/html\">");
		out.print("</HEAD>");
		out.print("<BODY>");
		out.print("<P>");
		out.print("<!-- send to game window and check there-->");
		out.print("<DIV align=\"center\"><FONT size=\"15\">");
		out.print("Welcome<br>");
		out.print("You know what to do:");
		out.print("<form action=\"register\" method=\"post\">");
		out.print("Name: <input type=\"text\" name=\"user\"><br>");
		out.print("Password: <input type=\"password\" name=\"pass\"><br>");
		out.print("Repeat_Password: <input type=\"password\" name=\"re_pass\"><br>");
		out.print("<input type=\"submit\" value=\"Register\">");
		if(somethingWrong)
			out.print("<p style=\"color:#FF0000\"><FONT size=\"10\">Somting Wrong</p>");
		out.print("</form>");
		out.print("</FONT></DIV>");
		out.print("<P></P>");
		out.print("</BODY></HTML>");
	}

}
